package org.example.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * author:
 * Date:2024/10/3017:30
 **/
public class StreamDemo {
    public static void main(String[] args) {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("key1", "objectObjectHashMap-1");
        objectObjectHashMap.put("key2", "objectObjectHashMap-2");
        objectObjectHashMap.put("key3", "objectObjectHashMap-3");
        objectObjectHashMap.put("key4", "objectObjectHashMap-4");

        Map<String, Object> objectObjectHashMap1 = new HashMap<>();
        objectObjectHashMap1.put("key1", "objectObjectHashMap1-1");
        objectObjectHashMap1.put("key2", "objectObjectHashMap1-2");
        objectObjectHashMap1.put("key3", "objectObjectHashMap1-3");
        objectObjectHashMap1.put("key4", "objectObjectHashMap1-4");

        Map<String, Object> objectObjectHashMap2 = new HashMap<>();
        objectObjectHashMap2.put("key1", "objectObjectHashMap2-1");
        objectObjectHashMap2.put("key2", "objectObjectHashMap2-2");
        objectObjectHashMap2.put("key3", "objectObjectHashMap2-3");
        objectObjectHashMap2.put("key4", "objectObjectHashMap2-4");

        List<Map<String, Object>> objects = new ArrayList<>();
        objects.add(objectObjectHashMap);
        objects.add(objectObjectHashMap1);
        objects.add(objectObjectHashMap2);
        System.out.println("-------------------------------------------");
        //找到List对象里面的某个字段组成List集合
        List<String> repairOrderCodes = objects.stream().map(item -> item.get("key1").toString())
                .filter(Objects::nonNull) // 过滤掉 null 值
                .filter(code -> !code.isEmpty() ) // 过滤掉空字符串
                .collect(Collectors.toList());
        repairOrderCodes.forEach(code -> System.out.println(code));
        System.out.println("--------------------0----------------------");

        //找到List对象里面的某个字段对  组成List集合
        // 比如说是 门店code
        // dlrCode  CWH01
        // dlrName  武汉店
        // 变成 ("CWH01","武汉店")
        Map<String, Object> dlrMap = objects.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("key1"),  // key
                        map -> (String) map.get("key2")   // value
                ));

        //使用Java 8的forEach方法和Lambda表达式
        dlrMap.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
        System.out.println("--------------------1----------------------");



        List<Map<String, Object>> key1 = objects.stream().filter(item -> item.get("key1") != null).collect(Collectors.toList());
        key1.forEach(item -> {
            //循环entrySet 键值对
            for (Map.Entry<String, Object> entry : item.entrySet()) {
                System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
            }
        });
        System.out.println("--------------------2----------------------");

        key1.forEach(item -> {
            //循环keySet 键对
            for (String key : item.keySet()) {
                System.out.println("Key: " + key + ", Value: " + item.get(key));
            }

        });
        System.out.println("--------------------3----------------------");

        key1.forEach(item -> {
            //使用迭代器
            Iterator<Map.Entry<String, Object>> iterator = item.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
            }

        });
        System.out.println("--------------------4----------------------");

        key1.forEach(item -> {
            //使用Java 8的forEach方法和Lambda表达式
            item.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
        });

        System.out.println("--------------------5----------------------");

        key1.forEach(item -> {
            //使用Java 8的Map.Entry流
            item.entrySet().stream()
                    .forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
        });
        System.out.println("--------------------6----------------------");

        key1.forEach(item -> {
            //使用Java 8的Stream API进行更复杂的操作
            item.entrySet().stream()
                    .filter(entry -> entry.getValue() instanceof String) // 过滤出值为String类型的条目
                    .forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
        });

        System.out.println("--------------------7----------------------");








    }
}
